<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
    	$this->login();
    }
    public function login(){ 
    	//商家后台登录
    	//判断shopid 是否存在
    	if(!empty($_SESSION['shopid'])){
    		$this->redirect('Admin/index');
    	}else{
    		$this->display('login');
    	}
    	
    }
    //验证用户名和密码
    public function verifyUser(){
    	// 检测验证码
		    $code=I('post.vercode','','strip_tags');

		    $data['username']=I('post.username','','strip_tags');
		    $data['password']=md5(I('post.password','','strip_tags'));
		    $data['_logic']='AND';
		    $verify = new \Think\Verify();
		    $result['vercode']=$verify->check($code);		    
		    if(empty($result['vercode'])){
               echo "<script type='text/javascript'>alert('验证码错误请重新填写'); window.history.go(-1);window.location.reload();</script>";
		    }

		    //验证用户名是否存在
		    $result['password']= M('shop_user')->where("username='%s'",$data['username'])->getField('password');
		    if(empty($result['password'])){
                echo "<script type='text/javascript'>alert('用户名不存在'); window.history.go(-1);window.location.reload();</script>";
		    }else{
               $result['shopid']=M('shop_user')->where($data)->getField('shopid');
               if(empty($result['shopid'])){
                    
                    echo "<script type='text/javascript'>alert('密码错误'); window.history.go(-1);window.location.reload();</script>";
               }else{

                 
               	 $_SESSION['username']=$data['username'];
               	 $_SESSION['shopid']=$result['shopid'];
               	// R('Adminlog/addlog',  array(array('登录','后台')));
               	$this->redirect('Admin/index');
               }
		    }

    }
    public function logout(){

    	if(!empty($_SESSION['shopid'])){
            
			$_SESSION=array();
			$this->login();
		}else{
			$this->login();
		}
    }
    //生成验证码
	public function vercode(){
		$Verify = new \Think\Verify();
		$Verify->fontSize =35;
		$Verify->length   = 4;
		$Verify->useNoise = false;
        $Verify->entry();
	}

	//空操作
	public function _empty(){
		$this->login();
	}
}